<?php

use Illuminate\Database\Seeder;

class MovieWatcherTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        ini_set('memory_limit', -1);
        $user_ids = App\Models\User::all()->pluck('id')->toArray();
        $movie_ids = App\Models\Movie::all()->pluck('id')->toArray();
        $i = 1;
        foreach (collect($user_ids)->chunk(1000) as $users) {
            $data = [];
            foreach ($users as $user) {
                $movie_id = collect($movie_ids)->random(5);
                foreach ($movie_id as $m) {
                    $status = (rand(1,3) < 3) ? 'watching' : 'watched';
                    $count = $status === 'watched' ? 1 : 0;
                    $data[] = [
                        'user_id' => $user,
                        'movie_id' => $m,
                        'status' => $status,
                        'updated_at' => \Carbon\Carbon::now(),
                        'created_at' => \Carbon\Carbon::now(),
                        'count' => $count,
                    ];
                }
            }
            \DB::table('movie_watch_user')->insert($data);
            $i++;
            print('第 ' . $i . '次处理完成！');
        }
    }
}
